package com.syni.mdd.sjb.common.datatreaure.listen.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppServiceOrder;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppServiceOrderService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Component
public class OrderListenerInit {
    @Autowired
    DxshAppServiceOrderService dxshAppServiceOrderService;

    @PostConstruct
    public void init(){
        Date nowTime = new Date();
        Integer pageNum = 1;
        Integer pageSize = 100;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppServiceOrder.STATUS,Constants.COMMON_FLAG_FALSE_INT);
        queryWrapper.eq(DxshAppServiceOrder.ORDER_TYPE,Constants.SERVICE_ORDER_SET);
        queryWrapper.eq(DxshAppServiceOrder.FIRST_INFO_STATUS, Constants.COMMON_FLAG_FALSE_INT);
        queryWrapper.ge(DxshAppServiceOrder.NEW_TIME,DateTimeUtils.converDateToLocalDateTime(DateTimeUtils.getStartTimeOfDay(nowTime)));
        while (true) {
            Page<DxshAppServiceOrder> page  = new Page(pageNum,pageSize);
            Page<DxshAppServiceOrder> dxshAppServiceOrdersPage = dxshAppServiceOrderService.page(page,queryWrapper);
            List<DxshAppServiceOrder> dxshAppServiceOrders = dxshAppServiceOrdersPage.getRecords();
            if(CollectionUtils.isNotEmpty(dxshAppServiceOrders)){
                for(DxshAppServiceOrder dxshAppServiceOrder : dxshAppServiceOrders){
                    dxshAppServiceOrderService.addInfo("_1", DateTimeUtils.addMinutes(DateTimeUtils.converLocalDateTimeToDate(dxshAppServiceOrder.getNewTime()),1),dxshAppServiceOrder,null);
                }
                pageNum++;
            }else {
                break;
            }
        }
        pageNum = 1;
        queryWrapper.clear();
        queryWrapper.eq(DxshAppServiceOrder.STATUS,Constants.COMMON_FLAG_FALSE_INT);
        queryWrapper.eq(DxshAppServiceOrder.ORDER_TYPE,Constants.SERVICE_ORDER_SET);
        queryWrapper.ge(DxshAppServiceOrder.NEW_TIME,DateTimeUtils.converDateToLocalDateTime(DateTimeUtils.getStartTimeOfDay(nowTime)));
        queryWrapper.eq(DxshAppServiceOrder.SECOND_INFO_STATUS, Constants.COMMON_FLAG_FALSE_INT);
        while (true) {
            Page<DxshAppServiceOrder> page  = new Page(pageNum,pageSize);
            Page<DxshAppServiceOrder> dxshAppServiceOrdersPage = dxshAppServiceOrderService.page(page,queryWrapper);
            List<DxshAppServiceOrder> dxshAppServiceOrders = dxshAppServiceOrdersPage.getRecords();
            if(CollectionUtils.isNotEmpty(dxshAppServiceOrders)){
                for(DxshAppServiceOrder dxshAppServiceOrder : dxshAppServiceOrders){
                    dxshAppServiceOrderService.addInfo("_2", DateTimeUtils.addMinutes(DateTimeUtils.converLocalDateTimeToDate(dxshAppServiceOrder.getNewTime()),2),dxshAppServiceOrder,null);
                }
                pageNum++;
            }else {
                break;
            }
        }
    }
}
